In [1]:
# -*- coding: utf-8 -*-
"""
Created on 2023/1/31 14:14

@author: Susan
"""
import pandas as pd

from pt_utils.PairTrading import PairTrading

trans_start = '2019-01-01'
end_date = '2022-01-01'
out_folder = 'pt_result/'
n_rev, n_reva = 0, 0
col = ['stock_0', 'stock_1', '配对系数', '已平仓实现收益', '总盈亏', 'entry_level', 'exit_level', 'trading_tlist']
nres_df = pd.DataFrame(columns=col)
while trans_start <= end_date:
    pt = PairTrading(trans_start, trans_start, 'pt_result/', c=0.0015, c_ratio=0.0015, pair_num=20)
    res_df, rev, reva = pt.run()
    nres_df = pd.concat([nres_df, res_df], ignore_index=True)
    n_rev += rev
    n_reva += reva
    trans_start = pt.trans_end
res_r = [None, None, None, n_rev, n_reva, None, None, None]
res_dict = dict(zip(col, res_r))
nres_df = nres_df.append(res_dict, ignore_index=True)
print(nres_df)
nres_df.to_csv(out_folder + '交易收益汇总.csv', index=False)
formation start	2018-01-01	formation end	2019-01-01
transaction start	2019-01-01	transaction end	2019-07-01
RESULTS will be saved at pt_result//2019-01-01_1Y_2019-01-01_6M/
100%|██████████| 32/32 [00:23<00:00,  1.35it/s]
100%|██████████| 20/20 [00:05<00:00,  3.85it/s]
formation start	2018-07-01	formation end	2019-07-01
transaction start	2019-07-01	transaction end	2020-01-01
RESULTS will be saved at pt_result//2019-07-01_1Y_2019-07-01_6M/
100%|██████████| 32/32 [00:33<00:00,  1.04s/it]
100%|██████████| 20/20 [00:05<00:00,  3.69it/s]
formation start	2019-01-01	formation end	2020-01-01
transaction start	2020-01-01	transaction end	2020-07-01
RESULTS will be saved at pt_result//2020-01-01_1Y_2020-01-01_6M/
100%|██████████| 32/32 [00:36<00:00,  1.13s/it]
100%|██████████| 20/20 [00:05<00:00,  3.96it/s]
formation start	2019-07-01	formation end	2020-07-01
transaction start	2020-07-01	transaction end	2021-01-01
RESULTS will be saved at pt_result//2020-07-01_1Y_2020-07-01_6M/
100%|██████████| 32/32 [00:39<00:00,  1.25s/it]
100%|██████████| 20/20 [00:05<00:00,  3.69it/s]
formation start	2020-01-01	formation end	2021-01-01
transaction start	2021-01-01	transaction end	2021-07-01
RESULTS will be saved at pt_result//2021-01-01_1Y_2021-01-01_6M/
100%|██████████| 32/32 [00:40<00:00,  1.25s/it]
100%|██████████| 20/20 [00:05<00:00,  3.59it/s]
formation start	2020-07-01	formation end	2021-07-01
transaction start	2021-07-01	transaction end	2022-01-01
RESULTS will be saved at pt_result//2021-07-01_1Y_2021-07-01_6M/
100%|██████████| 32/32 [00:46<00:00,  1.45s/it]
100%|██████████| 20/20 [00:05<00:00,  3.94it/s]
formation start	2021-01-01	formation end	2022-01-01
transaction start	2022-01-01	transaction end	2022-07-01
RESULTS will be saved at pt_result//2022-01-01_1Y_2022-01-01_6M/
100%|██████████| 32/32 [00:58<00:00,  1.82s/it]
100%|██████████| 20/20 [00:06<00:00,  3.23it/s]
       stock_0     stock_1        配对系数      已平仓实现收益          总盈亏  entry_level  \
0    603277.SH   603321.SH    1.133581     2.507954     1.479894    -0.021009   
1    300639.SZ   300676.SZ    0.803833     5.027589     5.027589    -0.033275   
2    600405.SH   600152.SH    1.240165          0.0    -7.368431     -0.03652   
3    000710.SZ   601965.SH    2.023435          0.0   -84.213657    -0.038621   
4    300669.SZ   603331.SH    1.083117     1.446763    -1.164487    -0.014075   
..         ...         ...         ...          ...          ...          ...   
143  002950.SZ   300888.SZ    0.688006            0            0    -0.030945   
144  002493.SZ   600346.SH    0.998592    13.872489    13.872489    -0.021739   
145  002141.SZ   002806.SZ    1.160418     0.786193     0.786193    -0.008139   
146  formation  2021-01-01  2022-01-01    66.701441   129.118236  transaction   
147        NaN         NaN         NaN  1717.022085  1272.848712          NaN   

     exit_level                                      trading_tlist  
0      0.021009  [2019-01-16 00:00:00, 2019-01-17 00:00:00, 201...  
1      0.033275         [2019-03-04 00:00:00, 2019-03-13 00:00:00]  
2       0.03652                              [2019-01-02 00:00:00]  
3      0.038621                              [2019-01-02 00:00:00]  
4      0.014075  [2019-01-02 00:00:00, 2019-05-20 00:00:00, 201...  
..          ...                                                ...  
143    0.030945                                                 []  
144    0.021739         [2022-04-11 00:00:00, 2022-06-13 00:00:00]  
145    0.008139         [2022-04-18 00:00:00, 2022-04-21 00:00:00]  
146  2022-01-01                                         2022-07-01  
147         NaN                                                NaN  

[148 rows x 8 columns]